package utils

import (
	"crypto/md5"
	"encoding/base64"
	"encoding/hex"
	"golang.org/x/crypto/bcrypt"
	"process/global"
)

// BcryptHash 使用 bcrypt 对密码进行加密
func BcryptHash(password string) string {
	bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	return string(bytes)
}

// BcryptCheck 对比明文密码和数据库的哈希值
func BcryptCheck(password, hash string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
	return err == nil
}

func Md5Check(password, hash string) bool {
	m5 := md5.New()
	_, err := m5.Write([]byte(password))
	if err != nil {
		return false
	}
	md5String := hex.EncodeToString(m5.Sum(nil))
	return md5String == hash
}

//安卓校验apiKey
func IsThrough(appKey string) bool {
	m5 := md5.New()
	_, err := m5.Write([]byte(global.GVA_CONFIG.System.ApiKey))
	if err != nil {
		return false
	}
	md5String := hex.EncodeToString(m5.Sum(nil))
	base64Str := base64.StdEncoding.EncodeToString([]byte(md5String))
	return base64Str == appKey
}
